package com.sean.common.databases;

import java.lang.reflect.Field;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

/**
 * SQL工具
 * @author sean
 *
 */
public class SQLUtil {
	
	public static final String TAG = "SQLUtil";
	
	/**
	 * 获取建表语句
	 * @param cla
	 * @return
	 */
	public static String getCreatTableSQL(Class<?> cla) {
		
		TableMapping tableMapping = cla.getAnnotation(TableMapping.class);
		if(tableMapping == null) {
			return null;
		}
		String tableName = tableMapping.tableName();
		
		StringBuffer sql = new StringBuffer("Create table ");
		sql.append("'" + tableName + "'");
		sql.append(" ( ");
		
		Field[] fields = cla.getDeclaredFields();
		ColumnMapping primaryColumnMapping = null;
		for(int i = 0; i < fields.length; i ++) {
			Field field = fields[i];
			ColumnMapping columnMapping = field.getAnnotation(ColumnMapping.class);
			if(columnMapping != null) {
				if(i > 0) {
					sql.append(" , ");
				}
				sql.append(" '" + columnMapping.columnName() + "' " 
						+ columnMapping.columnType() + " ");
				if(columnMapping.isPrimary()){
					primaryColumnMapping = columnMapping;
				}
			}
		}
		if(primaryColumnMapping!=null){
			sql.append(" , primary key (`" + primaryColumnMapping.columnName() + "`) ");
		}
		
		sql.append(" ); ");
		Log.i(TAG, "Table SQL >> " + sql.toString());
		return sql.toString();
	}
	
	/**
	 * 获取表名
	 * @param 实体类对象
	 * @return
	 */
	public static String getTableName(Class<?> cla) {
		String tableName = null;
		TableMapping tableMapping = cla.getAnnotation(TableMapping.class);
		if(tableMapping != null) {
			tableName = tableMapping.tableName();
		}
		return tableName;
	}
	
	public static ContentValues beanToContentValues(Object obj) {
		ContentValues cv = new ContentValues();
		Class<?> cla = obj.getClass();
		Field[] fields = cla.getDeclaredFields();
		
		for(Field field : fields){
			field.setAccessible(true);
			ColumnMapping columnMapping = field.getAnnotation(ColumnMapping.class);
			if(columnMapping != null){
				String columnName = columnMapping.columnName();
				Object value;
				try {
					value = field.get(obj);
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
				cv.put(columnName, value.toString());
			}
		}
		return cv;
	}
	
	public static boolean cursorToBean(Cursor cursor, Object obj) {
		if(cursor.moveToFirst()) {
			Class<?> cla = obj.getClass();
			Field[] fields = cla.getDeclaredFields();
			try {
				for(Field field : fields) {
					ColumnMapping columnMapping = field.getAnnotation(ColumnMapping.class);
					Class<?> fieldType = field.getType();
					if(columnMapping != null) {
						field.setAccessible(true);
						String columnName = columnMapping.columnName();
						if(fieldType.isAssignableFrom(String.class)) {
							String value = cursor.getString(cursor.getColumnIndex(columnMapping.columnName()));
							field.set(obj, value);
						} else if(fieldType.isAssignableFrom(Integer.class) 
								|| fieldType.isAssignableFrom(int.class)) {
							Integer value = cursor.getInt(cursor.getColumnIndex(columnMapping.columnName()));
							field.set(obj, value);
						} else if(fieldType.isAssignableFrom(Float.class) 
								|| fieldType.isAssignableFrom(float.class)) {
							Float value = cursor.getFloat(cursor.getColumnIndex(columnMapping.columnName()));
							field.set(obj, value);
						} else if(fieldType.isAssignableFrom(Long.class) 
								|| fieldType.isAssignableFrom(long.class)) {
							Long value = cursor.getLong(cursor.getColumnIndex(columnMapping.columnName()));
							field.set(obj, value);
						} else if(fieldType.isAssignableFrom(Double.class) 
								|| fieldType.isAssignableFrom(double.class)) {
							Double value = cursor.getDouble(cursor.getColumnIndex(columnMapping.columnName()));
							field.set(obj, value);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			return true;
		}
		return false;
	}
	
	/**
	 * 通过Bean对象获取ID的值
	 */
	public static String getIdByBean(Object obj) {
		Class<?> cla = obj.getClass();
		Field[] fields = cla.getDeclaredFields();
		
		for(Field field : fields){
			field.setAccessible(true);
			ColumnMapping columnMapping = field.getAnnotation(ColumnMapping.class);
			if(columnMapping != null){
				if(columnMapping.isPrimary()) {
					try {
						return field.get(obj).toString();
					} catch (Exception e) {
						e.printStackTrace();
						return null;
					}
				}
			}
		}
		return null;
	}
}
